<template>
  <el-dialog
    title="新增主播"
    :show-close="false"
    @close="closeDialog"
    width="1000px"
    :visible.sync="dialogVisible"
    :destroy-on-close="true"
  >
    <div class="form-box">
      <el-form
        ref="ruleForm"
        :inline="true"
        :model="formInline"
        :rules="rules"
        class="demo-form-inline"
      >
        <!-- 输入搜索部分 -->
        <el-form-item label="用户搜索">
          <el-input
            clearable
            v-model="formInline.dpAccount"
            placeholder="请输入大鹏号"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item class="s-btn">
          <el-button type="success" @click="searchUser">
            搜索
          </el-button>
        </el-form-item>
      </el-form>
      <el-table :data="userList" stripe style="width: 100%">
        <el-table-column prop="dpAccount" label="大鹏号" width="180">
        </el-table-column>
        <el-table-column prop="userName" label="昵称" width="180">
        </el-table-column>
        <el-table-column label="验证手机号" width="180">
          <template slot-scope="scope">
            <span>{{
              scope.row.mobileValidateState === 'Verification'
                ? scope.row.mobile
                : '-------'
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="value" label="分类">
          <template>
            <el-select
              @change="searchUser()"
              v-model="liveType"
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="item in liveTypes"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 表单部分 -->
    <span slot="footer" class="dialog-footer">
      <el-button type="success" plain @click="closeDialog">取 消</el-button>
      <el-button type="success" @click="postAnchor">确认</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { postAnchor } from '@/api/anchor/anchor-list'
import { getLiveTypes } from '@/api/anchor/live-type'
import { searchUsers } from '@/api/common/user'
export default {
  props: ['dialogVisible'],
  data() {
    return {
      loading: false,
      // 表单数据
      ruleForm: {
        type: '', // 1图文  2小视频
        collegeId: '', // 学院
        title: '' // 表单域内容
      },
      typeList: [], // 类型
      collegeList: [], // 学院数据
      maxLength: '', // 标题最大长度
      rules: {
        title: { required: true, message: '标题不能为空', trigger: 'blur' }
      },
      formInline: {
        dpAccount: '',
        type: 'dpAccount'
      },
      liveType: '',
      userList: [],
      liveTypes: []
    }
  },
  mounted() {
    getLiveTypes(
      { status: 'ENABLE', parentId: 0, pageSize: 100 },
      (response) => {
        this.liveTypes = response.rows
      },
      (response) => {
        this.$message({
          message: response.message,
          type: 'warning'
        })
      }
    )
  },
  methods: {
    closeDialog() {
      this.$emit('changeVisible', false)
    },
    postAnchor() {
      if (this.userList.length === 0) {
        this.$message({
          message: '请检索用户后操作',
          type: 'warning'
        })
        return false
      }
      if (!this.liveType) {
        this.$message({
          message: '请选择相关直播分类',
          type: 'warning'
        })
        return false
      }
      postAnchor(
        { userId: this.userList[0].userId, liveTypeId: this.liveType },
        (response) => {
          this.$emit('changeVisible', false, 'refresh')
        },
        (response) => {
          this.$message({
            message: response.message,
            type: 'warning'
          })
        }
      )
    },
    searchUser() {
      searchUsers(
        this.formInline,
        (response) => {
          if (!response.userId) {
            this.userList = []
            this.$message({
              message: '无搜索结果，请重新操作',
              type: 'warning'
            })
            return false
          }
          this.userList = [response]
        },
        (response) => {
          this.$message({
            message: response.message,
            type: 'warning'
          })
        }
      )
    }
  }
}
</script>
<style lang="less" scoped>
.addForm {
  .el-select {
    width: 500px;
  }
  .el-textarea {
    width: 350px;
  }
}
</style>
